package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ServiceItem;
import com.ruoyi.system.service.IServiceItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 服务项目Controller
 * 
 * @author 华馨悦
 * @date 2025-01-24
 */
@Controller
@RequestMapping("/system/item")
public class ServiceItemController extends BaseController
{
    private String prefix = "system/item";

    @Autowired
    private IServiceItemService serviceItemService;

    @RequiresPermissions("system:item:view")
    @GetMapping()
    public String item()
    {
        return prefix + "/item";
    }

    /**
     * 查询服务项目列表
     */
    @RequiresPermissions("system:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ServiceItem serviceItem)
    {
        startPage();
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        return getDataTable(list);
    }

    /**
     * 导出服务项目列表
     */
    @RequiresPermissions("system:item:export")
    @Log(title = "服务项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ServiceItem serviceItem)
    {
        List<ServiceItem> list = serviceItemService.selectServiceItemList(serviceItem);
        ExcelUtil<ServiceItem> util = new ExcelUtil<ServiceItem>(ServiceItem.class);
        return util.exportExcel(list, "服务项目数据");
    }

    /**
     * 新增服务项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务项目
     */
    @RequiresPermissions("system:item:add")
    @Log(title = "服务项目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ServiceItem serviceItem)
    {
        serviceItem.setCreatedTime(DateUtils.getNowDate());
        serviceItem.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(serviceItemService.insertServiceItem(serviceItem));
    }

    /**
     * 修改服务项目
     */
    @RequiresPermissions("system:item:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ServiceItem serviceItem = serviceItemService.selectServiceItemById(id);
        mmap.put("serviceItem", serviceItem);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务项目
     */
    @RequiresPermissions("system:item:edit")
    @Log(title = "服务项目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ServiceItem serviceItem)
    {
        serviceItem.setUpdatedTime(DateUtils.getNowDate());
        return toAjax(serviceItemService.updateServiceItem(serviceItem));
    }

    /**
     * 删除服务项目
     */
    @RequiresPermissions("system:item:remove")
    @Log(title = "服务项目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(serviceItemService.softDeleteServiceItemByIds(ids));
    }
}
